package com.tianqu.tms.core.taglib;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.taglib.TagUtils;
import org.apache.struts.taglib.html.ErrorsTag;

import javax.servlet.jsp.JspException;
import java.util.*;

public class TmsErrorsTag extends ErrorsTag {

    public int doStartTag() throws JspException {
        ActionMessages errors = null;

        try {
            errors = TagUtils.getInstance().getActionMessages(super.pageContext, this.name);
        } catch (JspException var11) {
            TagUtils.getInstance().saveException(super.pageContext, var11);
            throw var11;
        }

        if (errors != null && !errors.isEmpty()) {
            boolean headerPresent = TagUtils.getInstance().present(super.pageContext, this.bundle, this.locale, this.getHeader());
            boolean footerPresent = TagUtils.getInstance().present(super.pageContext, this.bundle, this.locale, this.getFooter());
            boolean prefixPresent = TagUtils.getInstance().present(super.pageContext, this.bundle, this.locale, this.getPrefix());
            boolean suffixPresent = TagUtils.getInstance().present(super.pageContext, this.bundle, this.locale, this.getSuffix());
            StringBuffer results = new StringBuffer();
            boolean headerDone = false;
            String message = null;
            Iterator reports = this.property == null ? errors.get() : errors.get(this.property);

            while(reports.hasNext()) {
                ActionMessage report = (ActionMessage)reports.next();
                if (!headerDone) {
                    if (headerPresent) {
                        message = TagUtils.getInstance().message(super.pageContext, this.bundle, this.locale, this.getHeader());
                        results.append(message);
                    }

                    headerDone = true;
                }

                /*<error>标签新加name属性*/
                if (prefixPresent) {
                    message = TagUtils.getInstance().message(super.pageContext, this.bundle, this.locale, this.getPrefix());
                    results.append(message);
                    results.append("  name='_"+report.getProperty()+"'  ");
                    results.append(" > ");
                }

                if (report.isResource()) {
                    message = TagUtils.getInstance().message(super.pageContext, this.bundle, this.locale, report.getKey(), report.getValues());
                } else {
                    message = report.getKey();
                }

                if (message != null) {
                    results.append(message);
                }

                if (suffixPresent) {
                    message = TagUtils.getInstance().message(super.pageContext, this.bundle, this.locale, this.getSuffix());
                    results.append(message);
                }
            }

            if (headerDone && footerPresent) {
                message = TagUtils.getInstance().message(super.pageContext, this.bundle, this.locale, this.getFooter());
                results.append(message);
            }

            TagUtils.getInstance().write(super.pageContext, results.toString());
            return 1;
        } else {
            return 1;
        }
    }





}
